package action

import (
	"chat-room/client/global"
	"chat-room/common"
	"encoding/json"
	"errors"
	"fmt"
)

type UserAction struct {

}

//登录
func (that *UserAction) Login(account string, password string) (err error){
	//组合数据
	var loginRequestMsgEx = common.LoginRequestMsg{
		Account: account,
		Password: password,
	}
	loginRequestMsgExByte, err := json.Marshal(loginRequestMsgEx)
	if err != nil {
		errStr := fmt.Sprintf("【登录请求的消息结构体】转json失败：%v",err)
		err = errors.New(errStr)
		return
	}
	messageEx := common.Message{
		Type: common.LoginRequestMsgType,
		Data: string(loginRequestMsgExByte),
	}
	//发送数据
	functiosEx := &common.Functions{}
	messageExByte, err := json.Marshal(messageEx)
	if err != nil {
		errStr := fmt.Sprintf("【传递消息结构体】转json失败：%v",err)
		err = errors.New(errStr)
		return
	}
	err = functiosEx.SendMsg(global.Conn,messageExByte)
	if err != nil {
		return
	}

	//接收返回结果
	msg, err :=functiosEx.ReadMsg(global.Conn)
	if err != nil {
		return
	}
	var loginResponseMsgEx  = common.LoginResponseMsg{}
	err = json.Unmarshal([]byte(msg.Data),&loginResponseMsgEx)
	if err != nil {
		errStr := fmt.Sprintf("【登录返回的消息结构体】转json失败：%v",err)
		err = errors.New(errStr)
		return
	}
	if loginResponseMsgEx.Code != 0 {
		err = errors.New(loginResponseMsgEx.Msg)
		return
	}
	//启一个协程，用来和服务端保持通讯
	keepEx := Keep{}
	go keepEx.handel()
	//显示登录后的菜单
	viewEx := &View{}
	viewEx.ShowAfterLoginMenu(loginResponseMsgEx.Name)
	return
}

//注册
func (that *UserAction) Register(account string, password string, name string) (err error){
	//组合数据
	var registerRequestMsgEx = common.RegisterRequestMsg{
		Account : account,
		Password : password,
		Name : name,
	}
	registerRequestMsgExByte, err := json.Marshal(registerRequestMsgEx)
	if err != nil {
		errStr := fmt.Sprintf("【注册请求的消息结构体】转json失败：%v",err)
		err = errors.New(errStr)
		return
	}

	var messageEx = common.Message{
		Type : common.RegisterRequestMsgType,
		Data : string(registerRequestMsgExByte),
	}
	//发送数据
	functiosEx := &common.Functions{}
	messageExByte, err := json.Marshal(messageEx)
	if err != nil {
		errStr := fmt.Sprintf("【传递消息结构体】转json失败：%v",err)
		err = errors.New(errStr)
		return
	}

	err = functiosEx.SendMsg(global.Conn, messageExByte)
	if err != nil {
		return
	}
	//接收返回结果
	msg, err :=functiosEx.ReadMsg(global.Conn)
	if err != nil {
		return
	}
	var RegisterResponseMsgEx  = common.RegisterResponseMsg{}
	err = json.Unmarshal([]byte(msg.Data),&RegisterResponseMsgEx)
	if err != nil {
		errStr := fmt.Sprintf("【注册返回的消息结构体】转json失败：%v",err)
		err = errors.New(errStr)
		return
	}
	if RegisterResponseMsgEx.Code != 0 {
		err = errors.New(RegisterResponseMsgEx.Msg)
		return
	}
	fmt.Println("注册成功")
	fmt.Println(RegisterResponseMsgEx)
	return
}

//获取在线用户列表
func (that *UserAction) GetOnlineUsers() (err error) {
	var messageEx = common.Message{
		Type : common.GetOnlineUsersRequestMsgType,
		Data : "",
	}
	//发送数据
	functiosEx := &common.Functions{}
	messageExByte, err := json.Marshal(messageEx)
	if err != nil {
		errStr := fmt.Sprintf("【传递消息结构体】转json失败：%v",err)
		err = errors.New(errStr)
		return
	}

	err = functiosEx.SendMsg(global.Conn, messageExByte)
	return
	//这里不接收返回的消息了，因为在登录后就由 协程：Keep.handel来保持通讯了
}